package com.hdhere.application.service;

import com.hdhere.application.Repository.UserInfoRepository;
import com.hdhere.application.Repository.permission.SysPermissionRepository;
import com.hdhere.application.Repository.permission.SysRoleRepository;
import com.hdhere.core.security.shiro.SecurityDataProvider;
import com.hdhere.domain.SysPermission;
import com.hdhere.domain.SysRole;
import com.hdhere.domain.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/*******************************************************************************************
 * 类描述：  
 *
 * @author: dustin
 * @date: 2019-04-27 13:49
 * @Description:
 * @version 1.0
 *
 *
 * Version    Date       ModifiedBy                 Content  
 * -------- ---------    ----------         ------------------------  
 * 1.0      2019/4/27       dustin                               
 *******************************************************************************************
 */
@Service
public class PermissionService implements SecurityDataProvider {

    @Autowired
    private SysPermissionRepository permissionRepository;

    @Autowired
    private SysRoleRepository roleRepository;

    @Autowired
    private UserInfoRepository userInfoRepository;

    @Override
    public UserInfo getUserWithPermission(String username) {
        UserInfo user = userInfoRepository.findByUsername(username);
        if(user == null) return null;
        List<SysRole> roles = roleRepository.getRoleWithPermission(user.getId());
        user.setRoleList(roles);
        return user;

        //        QUserInfo qUser = QUserInfo.userInfo;
//        QSysRole qRole = QSysRole.sysRole;
//        QSysUserRole qUserRole = QSysUserRole.sysUserRole;
//        QSysPermission qPermission = QSysPermission.sysPermission;
//        QSysRolePermission qRolePermission = QSysRolePermission.sysRolePermission;
//
//        UserInfo user = queryFactory.selectFrom(qUser)
//                .where(qUser.username.eq(userName))
//                .fetchOne();
//        if(user == null) return null;
//
//
////        Map<Long,UserInfo> map=
////        queryFactory.from(qUser)
////                .join(qUserRole).on(qUser.id.eq(qUserRole.userId))
////                .join(qRole).on(qUserRole.roleId.eq(qRole.id))
////                .where(qUser.username.eq(userName))
////                .transform(groupBy(qUserRole.userId).as(Projections.bean(UserInfo.class,qUser.id,qUser.username,qUser.password,qUser.salt,
////                        list(Projections.bean(SysRole.class, qRole.id,qRole.role
////                               // ,list(Projections.bean(SysPermission.class, qPermission.id,qPermission.name,qPermission.permission)).as("permissions")
////                            )
////                        ).as("roleList")
////                        )));
//        Map<Integer,SysRole> userRole=
//                queryFactory.from(qUser)
//                        .join(qUserRole).on(qUser.id.eq(qUserRole.userId))
//                        .join(qRole).on(qRole.id.eq(qUserRole.roleId))
//                        .join(qRolePermission).on(qRole.id.eq(qRolePermission.roleId))
//                        .join(qPermission).on(qPermission.id.eq(qRolePermission.permissionId))
//                        .where(qUser.id.eq(user.getId()))
//                        .transform(groupBy(qRole.id).as(Projections.bean(SysRole.class,qRole.id,qRole.name,qRole.role,qRole.available,
//                                list(Projections.bean(SysPermission.class, qPermission.id,qPermission.name,qPermission.permission)
//                                ).as("permissions")
//                        )));
//        Collection<SysRole> sysRoles = userRole.values();
//        user.setRoleList(sysRoles);
//        return user;
    }

    public List<SysPermission> getPermission(){
        return permissionRepository.findAll();
    }

    @Override
    public Map<String, String> getPermissionMap(){
        Map<String, String> list =
        getPermission()
                .stream()
                .collect(Collectors.toMap(x->x.getUrl(),x-> "perms["+x.getPermission()+"]"));
        return list;
    }



}
